*******************************************************************************
* Load candidates data
*******************************************************************************

use dataset_candidates, clear

*******************************************************************************
* Rescale ideology and presidential vote
*******************************************************************************

su ideology
replace ideology = (ideology - r(min))/(r(max) - r(min))
su trump
replace trump = (trump - r(min))/(r(max) - r(min))

*******************************************************************************
* Do the regressions with deputy, ideology, and incumbency
*******************************************************************************

eststo clear

eststo: reg voteshare deputy i.ncandidates, cl(district)
eststo: reg voteshare deputy ideology i.ncandidates, ///
	cl(district)
eststo: reg voteshare deputy ideology incumbent i.ncandidates, ///
	cl(district)

eststo: reg winner deputy i.ncandidates, cl(district)
eststo: reg winner deputy ideology i.ncandidates, ///
	cl(district)
eststo: reg winner deputy ideology incumbent i.ncandidates, ///
	cl(district)

*******************************************************************************
* Output
*******************************************************************************

label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable ideology "Candidate Conservatism"

#delimit;

esttab
	using "_output/tableS9_a.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		drop(*cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 0 1 0 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* Make interactions
*******************************************************************************

gen deputyXtrumpvoteshare = deputy*trumpvoteshare
gen ideologyXtrumpvoteshare = ideology*trumpvoteshare

*******************************************************************************
* Do the regressions with deputy and ideology
*******************************************************************************

eststo clear

eststo: reg voteshare ideology trumpvoteshare ideologyXtrumpvoteshare ///
	i.ncandidates, cl(district)	
eststo: reg voteshare deputy trumpvoteshare deputyXtrumpvoteshare ///
	i.ncandidates, cl(district)

eststo: reg winner ideology trumpvoteshare ideologyXtrumpvoteshare ///
	i.ncandidates, cl(district)	
eststo: reg winner deputy trumpvoteshare deputyXtrumpvoteshare ///
	i.ncandidates, cl(district)

*******************************************************************************
* Output
*******************************************************************************

label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable trumpvoteshare "Trump Vote Share"
label variable deputyXtrumpvoteshare "Experienced X Trump Vote"
label variable ideologyXtrumpvoteshare "Cand. Conservatism X Trump Vote"

#delimit;

esttab
	using "_output/tableS9_b.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		order(deputy ideology)
		drop(*cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 1 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* End
*******************************************************************************
